package org.jivesoftware.smackx.receipts;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DiscoverInfo;

public class DeliveryReceiptManager
  implements PacketListener
{
  private static Map<Connection, DeliveryReceiptManager> instances = Collections.synchronizedMap(new WeakHashMap());
  private Connection connection;
  private boolean auto_receipts_enabled = false;
  private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections.synchronizedSet(new HashSet());

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated(Connection paramAnonymousConnection)
      {
        DeliveryReceiptManager.getInstanceFor(paramAnonymousConnection);
      }
    });
  }

  private DeliveryReceiptManager(Connection paramConnection)
  {
    ServiceDiscoveryManager localServiceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(paramConnection);
    localServiceDiscoveryManager.addFeature("urn:xmpp:receipts");
    this.connection = paramConnection;
    instances.put(paramConnection, this);
    paramConnection.addPacketListener(this, new PacketExtensionFilter("urn:xmpp:receipts"));
  }

  public static synchronized DeliveryReceiptManager getInstanceFor(Connection paramConnection)
  {
    DeliveryReceiptManager localDeliveryReceiptManager = (DeliveryReceiptManager)instances.get(paramConnection);
    if (localDeliveryReceiptManager == null)
      localDeliveryReceiptManager = new DeliveryReceiptManager(paramConnection);
    return localDeliveryReceiptManager;
  }

  public boolean isSupported(String paramString)
  {
    try
    {
      DiscoverInfo localDiscoverInfo = ServiceDiscoveryManager.getInstanceFor(this.connection).discoverInfo(paramString);
      return localDiscoverInfo.containsFeature("urn:xmpp:receipts");
    }
    catch (XMPPException localXMPPException)
    {
    }
    return false;
  }

  public void processPacket(Packet paramPacket)
  {
    DeliveryReceipt localDeliveryReceipt = (DeliveryReceipt)paramPacket.getExtension("received", "urn:xmpp:receipts");
    Object localObject2;
    Object localObject1;
    if (localDeliveryReceipt != null)
    {
      localObject2 = this.receiptReceivedListeners.iterator();
      while (((Iterator)localObject2).hasNext())
      {
        localObject1 = (ReceiptReceivedListener)((Iterator)localObject2).next();
        ((ReceiptReceivedListener)localObject1).onReceiptReceived(paramPacket.getFrom(), paramPacket.getTo(), localDeliveryReceipt.getId());
      }
    }
    if (this.auto_receipts_enabled)
    {
      localObject1 = (DeliveryReceiptRequest)paramPacket.getExtension("request", "urn:xmpp:receipts");
      if (localObject1 != null)
      {
        localObject2 = new Message(paramPacket.getFrom(), Message.Type.normal);
        ((Message)localObject2).addExtension(new DeliveryReceipt(paramPacket.getPacketID()));
        this.connection.sendPacket((Packet)localObject2);
      }
    }
  }

  public void setAutoReceiptsEnabled(boolean paramBoolean)
  {
    this.auto_receipts_enabled = paramBoolean;
  }

  public void enableAutoReceipts()
  {
    setAutoReceiptsEnabled(true);
  }

  public void disableAutoReceipts()
  {
    setAutoReceiptsEnabled(false);
  }

  public boolean getAutoReceiptsEnabled()
  {
    return this.auto_receipts_enabled;
  }

  public void addReceiptReceivedListener(ReceiptReceivedListener paramReceiptReceivedListener)
  {
    this.receiptReceivedListeners.add(paramReceiptReceivedListener);
  }

  public void removeReceiptReceivedListener(ReceiptReceivedListener paramReceiptReceivedListener)
  {
    this.receiptReceivedListeners.remove(paramReceiptReceivedListener);
  }

  public static boolean hasDeliveryReceiptRequest(Packet paramPacket)
  {
    return paramPacket.getExtension("request", "urn:xmpp:receipts") != null;
  }

  public static void addDeliveryReceiptRequest(Message paramMessage)
  {
    paramMessage.addExtension(new DeliveryReceiptRequest());
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.receipts.DeliveryReceiptManager
 * JD-Core Version:    0.6.1
 */